package com.sduept.nwld.dataserver.sourceinfo.transform.impl;

import com.sduept.nwld.dataserver.sourceinfo.transform.SourceInfoTransform;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SourceInfoTransformImpl  implements SourceInfoTransform{

	@Override
	public String subString(String sourceInfo, String targetInfo) {
		if(sourceInfo.indexOf(targetInfo)!=-1){
			  int beginIndex=sourceInfo.indexOf(targetInfo);
			  int subLength=targetInfo.length();
			  return sourceInfo.substring(beginIndex,beginIndex+subLength);
		}
		return null;
	}

	@Override
	public String subString(String sourceInfo, boolean isFirst, char startChar,	int step) {
		if(sourceInfo.indexOf(startChar)!=-1){
			if(isFirst){
				if(step<=sourceInfo.length()-sourceInfo.indexOf(startChar))
					return sourceInfo.substring(sourceInfo.indexOf(startChar),sourceInfo.indexOf(startChar)+step);
			}
			else{
				if(step<=sourceInfo.length()-sourceInfo.lastIndexOf(startChar))
					return sourceInfo.substring(sourceInfo.lastIndexOf(startChar),sourceInfo.lastIndexOf(startChar)+step);
			}
		}
		return null;
	}

	@Override
	public String subString(String sourceInfo, Object fromIndex, boolean isFirst, char startChar, int step) {
		if(sourceInfo.indexOf(startChar)!=-1){
			if(fromIndex instanceof Character){
				if(isFirst){
					if(sourceInfo.indexOf(startChar,sourceInfo.indexOf((char)fromIndex))>=0){
						if(step<=sourceInfo.length()-sourceInfo.indexOf(startChar,sourceInfo.indexOf((char)fromIndex)))
							return sourceInfo.substring(sourceInfo.indexOf(startChar,sourceInfo.indexOf((char)fromIndex)),
								sourceInfo.indexOf(startChar,sourceInfo.indexOf((char)fromIndex))+step);
					}
				}
				else {
					if(sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf((char)fromIndex))>=0){
						if(step<=sourceInfo.length()-sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf((char)fromIndex)))
							return sourceInfo.substring(sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf((char)fromIndex)),
								sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf((char)fromIndex))+step);
					}
				}
			}else if(fromIndex instanceof String){
				if(isFirst){
					if(sourceInfo.indexOf(startChar,sourceInfo.indexOf(fromIndex.toString()))>=0){
						if(step<=sourceInfo.length()-sourceInfo.indexOf(startChar,sourceInfo.indexOf(fromIndex.toString())))
							return sourceInfo.substring(sourceInfo.indexOf(startChar,sourceInfo.indexOf(fromIndex.toString())),
								sourceInfo.indexOf(startChar,sourceInfo.indexOf(fromIndex.toString()))+step);
					}
				}
				else {
					if(sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf(fromIndex.toString()))>=0){
						if(step<=sourceInfo.length()-sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf(fromIndex.toString())))
							return sourceInfo.substring(sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf(fromIndex.toString())),
								sourceInfo.lastIndexOf(startChar,sourceInfo.indexOf(fromIndex.toString()))+step);
					}
				}
			}
		}
		return null;
	}
	
	@Override
	public String concatString(char connector,String... targetInfos) {
		StringBuilder concatStr=new StringBuilder();
		if(targetInfos!=null && targetInfos.length>0){
			if(String.valueOf(connector)!=null && !"".equals(String.valueOf(connector).trim())){
				for(String targetInfo:targetInfos){
					concatStr.append(targetInfo).append(String.valueOf(connector));
				}
			}else{
				for(String targetInfo:targetInfos){
					concatStr.append(targetInfo);
				}
			}
		}
		return concatStr.toString();
	}
	
	@Override
	public String[] splitString(String separator, String sourceInfo) {
		String[] targetInfos=null;
		if(sourceInfo!=null && !"".equals(sourceInfo)){
			if(separator!=null && !"".equals(separator))
				targetInfos=sourceInfo.split(separator);
		}
		return targetInfos;
	}


	@Override
	public String replace(String sourceInfo, int replaceType, Object oldInfo, Object newInfo) {
		String targetInfo=null;
		if(oldInfo instanceof Character){
			if(sourceInfo.indexOf((char)oldInfo)!=-1){
				if(replaceType==1){
					if(newInfo instanceof Character)
						targetInfo=sourceInfo.replaceFirst(String.valueOf(oldInfo), String.valueOf(newInfo));	
					else if(newInfo instanceof String)
						targetInfo=sourceInfo.replaceFirst(String.valueOf(oldInfo), newInfo.toString());	
				}else if(replaceType==2){
					if(newInfo instanceof Character)
						targetInfo=sourceInfo.replace((char)oldInfo, (char)newInfo);
					else if(newInfo instanceof String)
						this.replace(sourceInfo, String.valueOf(oldInfo), newInfo);
				}
			}
		}else if(oldInfo instanceof String){
			if(sourceInfo.indexOf(oldInfo.toString())!=-1){
				if(replaceType==1){					
					targetInfo=sourceInfo.replaceFirst(String.valueOf(oldInfo), String.valueOf(newInfo));				
				}else if(replaceType==2){
					if(newInfo instanceof Character)
						targetInfo=sourceInfo.replace(oldInfo.toString(), String.valueOf(newInfo));
					else if (newInfo instanceof String)
						targetInfo=sourceInfo.replace(oldInfo.toString(),newInfo.toString());
					
				}
			}
		}
		return targetInfo;
	}

	@Override
	public String replace(String sourceInfo, String regEx, Object newInfo) {
		String targetInfo=null;
		if(newInfo instanceof Character){			
			targetInfo=sourceInfo.replaceAll(regEx, String.valueOf(newInfo));			
		}else if(newInfo instanceof String){
			targetInfo=sourceInfo.replaceAll(regEx, newInfo.toString());	
		}
		return targetInfo;
	}

	@Override
	public Object typeConversion(String sourceInfo, String... targetInfos) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static void main(String[] args){
		  SourceInfoTransformImpl sitfi=new SourceInfoTransformImpl();
		  String string= "你好，早上要记得吃饭";		
		  char sub='你';
		  int step=1;
		  String result1=sitfi.subString(string,true,sub,step);
		  log.info(result1);
		  
		  String fromIndex="得";
		  char sub2='记';
		  int step2=2;
			
		  String result2=sitfi.subString(string,fromIndex,false,sub2,step2);
		  log.info(result2);
		  
		  String subString="吃饭";
		  String result3=sitfi.subString(string, subString);
		  log.info(result3);
		  
		  String result=sitfi.concatString(' ',result1,result2,result3);
		  log.info(result);
		  
		  String targetInfo= sitfi.replace("abcde5fe3sec", 2, "e", "rrr");
		  log.info(targetInfo);
		  
		  String targetInfo2= sitfi.replace("abcde5fe3sec", "\\d",  "***");
		  log.info(targetInfo2);
		  
		  
		  String[] targetInfo3=sitfi.splitString(",", "你好,leo,请加油！");
		  for(String _targetInfo:targetInfo3){
			  log.info(_targetInfo);
		  }
		  
	}

	
	

	

}
